<?php
// script auto update into host_snmp_cache
include(dirname(__FILE__) . "/../../../include/global.php");
include(dirname(__FILE__) . "/../model/nms_script_model.php");
include(dirname(__FILE__) . "/../model/nms_alarm_model.php");
// get host 
$host = db_fetch_assoc('select id, hostname,description, snmp_community
							from host a, plugin_nms_interface b
							where a.id= b.host_id and a.status != 1 group by id');
$check_host =db_fetch_assoc('SELECT host_id from plugin_nms_interface group by host_id');
	if(!empty($check_host)){
		$count =0;
		foreach($host as $item){
			// get interface and oid to sent snmp v2 to devices
			$inter = db_fetch_assoc('SELECT interface_id, nms_status, status_oid, alias_oid
						FROM plugin_nms_interface
						WHERE host_id = '.$item['id'].'
							and present =1');
			foreach($inter as $item1){
				// get status
				$status = snmp2_get($item['hostname'],$item['snmp_community'],$item1['status_oid']);
				$tem = substr($status, -2,1);
				if($tem == 1){
					$status = "Up";
				}else{
					if($tem == 2){
						$status = "Down";
					}else{
						$status = "Unknown";
					}
				}
				// get alias
				$alias = snmp2_get($item['hostname'],$item['snmp_community'],$item1['alias_oid']);
				$alias = str_replace('STRING: ', '', $alias);
				$alias = trim($alias, '" "');
				// update alias into nms, plugin_nms_interface
				update_alias_interface($alias, $item1['interface_id']);
				//update status into cacti, host_snmp_cache
				update_field_value_host_snmp_cache($status,$item['id'],$item1['status_oid']);
				//update alias into cacti, host_snmp_cache
				update_field_value_host_snmp_cache($alias,$item['id'],$item1['alias_oid']);
			}
			$count++;	
		}
			cacti_log('Network Monitoring System (Autoupdate): has updated ' .$count. ' devices',false);
		//auto pause
		// get interface down and down time > 12h to pause
		$count1 =0;
		$interface = db_fetch_assoc('select interface_id from 
															(select * from
																( select * from plugin_nms_log order by nms_time desc) as a group by interface_id) as b 
									where b.nms_status = "Down" 
										and time_to_sec((timediff(now(),b.nms_time)))/(60*60) >12');
		foreach ($interface as $item){
			$count1++;
			script_update_pause_interface($item['interface_id']);
		}
		script_update_resume_all();
		cacti_log('Network Monitoring System (Autoupdate): had paused ' .$count1. 'interface');
	}else{
		cacti_log('Network Monitoring System (Autoupdate): host does not exist ', false,'NMS');
	}

